package com.JobFind.logic.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


import com.JobFind.DAO.IUserDAO;
import com.JobFind.DTO.UserDTO;
import com.JobFind.logic.IUserLogic;
import com.JobFind.logic.ILogLogic;
import com.JobFind.model.TbLog;
import com.JobFind.model.TbUser;
import com.opensymphony.xwork2.Result;

@Component
public class UserLogic implements IUserLogic {

	private IUserDAO userDAO;
    private IUserLogic userLogic;
    private ILogLogic loglogic;
	public IUserDAO getUserDAO() {
		return userDAO;
	}

	@Resource
	public void setUserDAO(IUserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public IUserLogic getUserLogic() {
		return userLogic;
	}

	@Resource
	public void setUserLogic(IUserLogic userLogic) {
		this.userLogic = userLogic;
	}

	
	public ILogLogic getLoglogic() {
		return loglogic;
	}

	@Resource
	public void setLoglogic(ILogLogic loglogic) {
		this.loglogic = loglogic;
	}

	@Override
	@Transactional
	public Integer loginCheck(String userLoginName, String userPwd) throws Exception {
		Integer result = 0;
		try {
			List<TbUser> userlist = userDAO.loginCheck(userLoginName);
			if (userlist == null) {
				result = 0;
			} else if (userlist.size() == 0) {
				result = 0;
			} else {
				TbUser userbean = (TbUser) userlist.get(0);
				if (!userPwd.equals(userbean.getUserPwd())) {
					result = 0;
				}
				else{
					HttpServletRequest request = ServletActionContext.getRequest();
					HttpSession loginuserSession = request.getSession();
					loginuserSession.setAttribute("loginusersession", userlist.get(0));
					result = userbean.getUserId();
					TbLog logBean = new TbLog();
					logBean.setTbUser(userlist.get(0));
					logBean.setLogIp(request.getRemoteAddr());
					SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					logBean.setLogTime(df.format(new Date()).toString());
					loglogic.addLog(logBean);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = -1;
		}
		return result;
	}
	
	@Override
	@Transactional
	public String getUserInfoById(Integer userId) throws Exception{
		String result = "";
		try {
			List<TbUser> userlist = userDAO.getUserInfoById(userId);
			if (userlist != null) {
				List<UserDTO> listUserDTO = new ArrayList<UserDTO>();
				Iterator<TbUser> itrUserBean = userlist.iterator();
				while (itrUserBean.hasNext()) {
					TbUser objUserBean = (TbUser) itrUserBean.next();

					listUserDTO.add(objUserBean.toDTO());
				}
				result = JSONArray.fromObject(listUserDTO).toString();
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = "";
		}
		return result;
	}
	
	@Override
	@Transactional
	public Integer logoutCheck(Integer userId) throws Exception{
		Integer result = 0;
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			request.getSession().removeAttribute("loginusersession");
			request.getSession().invalidate();
			result = 1;
		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}
	
	@Override
	@Transactional
	public Integer updateUserInfo(Integer userId,String homePlace,String birthday,String userNickName,String userNation) throws Exception{
		Integer result = 0;
		try {
			result = this.userDAO.updateUserInfo(userId,homePlace, birthday, userNickName, userNation);
		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}

	@Override
	@Transactional
	public Integer modifyUserPwd(Integer userId, String userPwd)
			throws Exception {
		Integer result = 0;
		try {
			result = this.userDAO.modifyUserPwd(userId, userPwd);
		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}

	@Override
	@Transactional
	public Integer addUser(UserDTO userDTO) throws Exception {
		Integer result = 0;
		try {
			result = this.userDAO.addUser(userDTO.toDAO());
		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}
	
}
